﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式之禅.中介者模式
{
    /// <summary>
    /// 抽象同事类
    /// </summary>
    public abstract class AbstractColleague
    {
        public int MoneyCount { get; set; }
        public abstract void ChangeMoney(int num, AbstractMediator mediator);
    }
    /// <summary>
    /// 具体同事A
    /// </summary>
    public class ColleagueA : AbstractColleague
    {
        public override void ChangeMoney(int num, AbstractMediator mediator)
        {
            mediator.AWin(num);
        }
    }
    /// <summary>
    /// 具体同事B
    /// </summary>
    public class ColleagueB : AbstractColleague
    {
        public override void ChangeMoney(int num, AbstractMediator mediator)
        {
            mediator.BWin(num);
        }
    }


    /// <summary>
    /// 抽象中介者
    /// </summary>
    public abstract class AbstractMediator
    {
        protected AbstractColleague colleagueA;
        protected AbstractColleague colleagueB;
        public AbstractMediator(AbstractColleague a, AbstractColleague b)
        {
            this.colleagueA = a;
            this.colleagueB = b;
        }
        public abstract void AWin(int count);
        public abstract void BWin(int count);
    }
    /// <summary>
    /// 具体中介者
    /// </summary>
    public class ConcreteMediator : AbstractMediator
    {
        public ConcreteMediator(AbstractColleague a, AbstractColleague b)
            : base(a, b)
        {
        }

        public override void AWin(int num)
        {
            this.colleagueA.MoneyCount += num;
            this.colleagueB.MoneyCount -= num;
        }
        public override void BWin(int num)
        {
            this.colleagueA.MoneyCount -= num;
            this.colleagueB.MoneyCount += num;
        }
    }


}
